package ck.mmd.dao;

import java.util.Date;
import java.util.List;








import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanHandler;
import org.apache.commons.dbutils.handlers.BeanListHandler;

import ck.mmd.bean.User;
import ck.mmd.util.DaoUtils;


public class UserDao {
	/**
	 * 登录
	 * @param user 封装的User类型对象
	 * @return  返回User类型对象
	 */
	public User login(User user) {
		String sql = "select * from user where username = ? and userpass = ? ";
		try{
			QueryRunner runner = new QueryRunner(DaoUtils.getSource());
			return runner.query(sql, new BeanHandler<User>(User.class), user.getUsername(),user.getUserpass());
		}catch(Exception e){
			e.printStackTrace();
			throw new RuntimeException(e);
		}
	}

	public void addUser(User user) {
		String sql = "insert into user values(?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)";
		try{
			QueryRunner runner = new QueryRunner(DaoUtils.getSource());
			runner.update(sql, user.getId(),user.getUsername(),user.getUserpass(),
					user.getEmail(),user.getPriced(),user.getPower(),
					user.getIssendsale(),user.getIssale_power(),
					user.getActivestate(),user.getActivecode(),user.getRegisttime(),
					user.getSendtime(),user.getSendoktime(),user.getLastlogintime(),
					user.getBackimg(),user.getPhone(),user.getLogoimg(),
					user.getSex(),user.getSaletype(),user.getRealname(),user.getSalename(),
					user.getAdopt_num()
					);
		}catch(Exception e){
			e.printStackTrace();
			throw new RuntimeException(e);
		}
	}

	public User findUserById(String id) {
		String sql = "select * from user where id = ?";
		try{
			QueryRunner runner = new QueryRunner(DaoUtils.getSource());
			return runner.query(sql,new BeanHandler<User>(User.class),id);
		}catch (Exception e) {
			e.printStackTrace();
			throw new RuntimeException(e);
		}
	}
	
	public List<User> findAllUser() {
		String sql = "select * from user  order by registtime desc";
		try{
			QueryRunner runner = new QueryRunner(DaoUtils.getSource());
			return runner.query(sql, new BeanListHandler<User>(User.class));
		}catch (Exception e) {
			e.printStackTrace();
			throw new RuntimeException(e);
		}
	}
	
	public void delete(String userId) {
		String sql = "delete from user where id = ?";
		try{
			QueryRunner runner = new QueryRunner(DaoUtils.getSource());
			runner.update(sql, userId);
		}catch(Exception e){
			e.printStackTrace();
			throw new RuntimeException(e);
		}
	}

	/**
	 * 查找激活用户
	 * @param activecode
	 * @return
	 */
	public User findUserByActive(String activecode) {
		String sql = "select * from user where activecode = ?";
		try{
			QueryRunner runner = new QueryRunner(DaoUtils.getSource());
			return runner.query(sql,new BeanHandler<User>(User.class),activecode);
		}catch (Exception e) {
			e.printStackTrace();
			throw new RuntimeException(e);
		}
	}

	/**
	 * 改变用户状态
	 * @param id
	 * @param str
	 * @return
	 */
	public String setState(String id, String str) {
		String sql = "update user set state =? where id = ?";
		try{
			QueryRunner runner = new QueryRunner(DaoUtils.getSource());
			runner.update(sql,str,id);
			return "1";
		}catch(Exception e){
			e.printStackTrace();
			throw new RuntimeException(e);
		}
	}
	
	
	/**
	 * 更新用户登录时间
	 * @param date
	 * @param id
	 */
	public void updateLoginTime(Date date,String id) {
		String sql = "update user set lastlogintime =? where id = ?";
		try{
			QueryRunner runner = new QueryRunner(DaoUtils.getSource());
			runner.update(sql,date,id);
		}catch(Exception e){
			e.printStackTrace();
			throw new RuntimeException(e);
		}
	}

	/**
	 * 添加用户认证信息
	 * @param user
	 */
	public void addRzInfo(User user) {
		String sql = "update user set saletype =? ,salename = ? , phone = ? , realname = ? , issendsale = ? ,sendtime = ? where id = ?";
		try{
			QueryRunner runner = new QueryRunner(DaoUtils.getSource());
			runner.update(sql,user.getSaletype(),user.getSalename(),user.getPhone(),user.getRealname(),user.getIssendsale(),user.getSendtime(), user.getId());
		}catch(Exception e){
			e.printStackTrace();
			throw new RuntimeException(e);
		}
	}

	/**
	 * 通过username查找用户
	 * @param name
	 * @return
	 */
	public User findUserByName(String name) {
		String sql = "select * from user where username = ?";
		try{
			QueryRunner runner = new QueryRunner(DaoUtils.getSource());
			return runner.query(sql,new BeanHandler<User>(User.class),name);
		}catch (Exception e) {
			e.printStackTrace();
			throw new RuntimeException(e);
		}
	}

	/**
	 * 更新用户发布的领养的数量
	 * @param i
	 * @param id
	 */
	public void updateAdopt_num(int i,String id) {
		String sql = "update user set adopt_num =? where id = ?";
		try{
			QueryRunner runner = new QueryRunner(DaoUtils.getSource());
			runner.update(sql,i,id);
		}catch(Exception e){
			e.printStackTrace();
			throw new RuntimeException(e);
		}
	}

	/**
	 * 查找提交申请并且还未通过审核的用户
	 * @return
	 */
	public List<User> findAllUserBySend() {
		String sql = "select * from user where power = 1 and issendsale = 1 and issale_power = 0";
		try{
			QueryRunner runner = new QueryRunner(DaoUtils.getSource());
			return runner.query(sql,new BeanListHandler<User>(User.class));
		}catch (Exception e) {
			e.printStackTrace();
			throw new RuntimeException(e);
		}
	}

	/**
	 * 查找所有管理员
	 * @return
	 */
	public List<User> findAllUserByAdmin() {
		String sql = "select * from user where power = 0";
		try{
			QueryRunner runner = new QueryRunner(DaoUtils.getSource());
			return runner.query(sql,new BeanListHandler<User>(User.class));
		}catch (Exception e) {
			e.printStackTrace();
			throw new RuntimeException(e);
		}
	}

	/**
	 * 查找已通过认证的用户
	 * @return
	 */
	public List<User> findAllBySended() {
		String sql = "select * from user where issale_power = 1";
		try{
			QueryRunner runner = new QueryRunner(DaoUtils.getSource());
			return runner.query(sql,new BeanListHandler<User>(User.class));
		}catch (Exception e) {
			e.printStackTrace();
			throw new RuntimeException(e);
		}
	}
	
	/**
	 * 查找已通过认证的用户
	 * @return
	 */
	public List<User> findAllByNormal() {
		String sql = "select * from user where power = 0 and issendsale = 0";
		try{
			QueryRunner runner = new QueryRunner(DaoUtils.getSource());
			return runner.query(sql,new BeanListHandler<User>(User.class));
		}catch (Exception e) {
			e.printStackTrace();
			throw new RuntimeException(e);
		}
	}

	/**
	 * 查找已经通过审核的用户
	 * @return
	 */
	public List<User> findAllUserBySended() {
		String sql = "select * from user where power = 1 and issendsale = 0 and issale_power = 1";
		try{
			QueryRunner runner = new QueryRunner(DaoUtils.getSource());
			return runner.query(sql,new BeanListHandler<User>(User.class));
		}catch (Exception e) {
			e.printStackTrace();
			throw new RuntimeException(e);
		}
	}

}
